<?php
session_start();

$_SESSION["g2ulogin"] = 2;
$_SESSION["g2uId"] = $_REQUEST["id"];
$_SESSION["g2uName"] = $_REQUEST["name"];
$_SESSION["g2uMail"] = $_REQUEST["email"];
if ($_SESSION["g2uMail"]== "undefined"){
    $_SESSION["g2uMail"] = "";
}
$_SESSION["g2uPassword"] = $_REQUEST["password"];

$_SESSION["g2uBirthday"] = $_REQUEST["birthday"];
if ($_SESSION["g2uBirthday"]== "undefined"){
    $_SESSION["g2uBirthday"] = "";
}
$_SESSION["g2uFirstName"] = $_REQUEST["fname"];
$_SESSION["g2uLastName"] = $_REQUEST["lname"];

$_SESSION["g2uProfilePic"] = $_REQUEST["picture"];

$_SESSION["g2uFacebookUserName"] = $_REQUEST["uname"];
$_SESSION["g2uFacebookProfileLink"] = $_REQUEST["link"];


include_once 'verificarLogin.php';

include_once 'recursos/config.php';
include_once 'recursos/image.php';
    
    $img = new image();
    $cfg = new configuracion();
    
$resultado = verificarExisteUsuario($_REQUEST["id"], $_REQUEST["type"], $_REQUEST["email"]);

if(($resultado == 0 && $_SESSION["g2uId"]>0) || ($resultado == 0 &&  $_REQUEST["type"]=="mail")){

    include_once 'modelo/modelo.user.php';
    include_once 'recursos/fechas.php';
    
    $date = new fechas();
    $user = new user();
    
    $user->set_user_id($_SESSION["g2uId"]);
    $user->set_user_name($_SESSION["g2uName"]);
    $user->set_user_fname($_SESSION["g2uFirstName"]);
    $user->set_user_lname($_SESSION["g2uLastName"]);
    $user->set_user_uname($_SESSION["g2uFacebookUserName"]);
    $user->set_user_sys_name($_SESSION["g2uFacebookUserName"]);
    $user->set_user_password($_SESSION["g2uPassword"]);
    $user->set_user_mail($_SESSION["g2uMail"]);
    $user->set_user_fb_link($_SESSION["g2uFacebookProfileLink"]);
    $user->set_user_g_id(3);
    $user->set_user_ut_id(2);
    $user->set_user_birthday($date->convertirFechatoMysql($_SESSION["g2uBirthday"]));
    $user->set_user_status(1);

    if ($_REQUEST["type"] == "facebook") {
        $id = registrarUsuarioDeFacebook($user);
        $inPath = $_REQUEST["picture"];
    }
    
    if ($_REQUEST["type"] == "mail") {
        $id = registrarUsuarioXCorreo($user);
        $inPath = "https://g2u.com.pe/images/png/imagencontacto.png";
    }
    
        $outPath = $cfg->getProfilePicureURLos().$id.".jpg";
        $img->save_image($inPath, $outPath);
        
        $user->set_user_profile_pic($cfg->getProfilePicureURLweb().$id.".jpg");
        actualizarProfilePicture($id, $user);

    if ($user->get_user_mail()!='') {
        include_once 'recursos/sendMail.php';
        $sendMail = new sendMail();
        $sendMail->confirmacion_nuevo_usuario($user->get_user_mail());
    }
    
    $resultado = 1;
    $_SESSION["g2uId"] =  $id;
    $_SESSION["g2uFirstTime"] = true;
    
}

if ($resultado > 0){
    cargarSesionUsuario($_SESSION["g2uId"], $_REQUEST["type"], $_REQUEST["email"], $_REQUEST["password"]);
    if ($_SESSION["g2uId"]>0){
        cargarSesionUsuarioFacebook($_SESSION["g2uId"]);
    } else {
        $_SESSION["g2ulogin"] = 1;
    }    
} else {
    $_SESSION["g2ulogin"] = 1;
}

?>
<html>
    <head>
        <script lang="javascript" type="text/javascript" src="js/jquery-1.9.1.js"></script>
        <script>
            $(document).ready(function(){
                setTimeout(function(){window.location.href = '<?=$cfg->getG2uDominio()?>';}, 800);            
            });
        </script>
    </head>
    <body>
    </body>
</html>
